package com.example.administrator.szb.view;

import android.app.Activity;
import android.graphics.drawable.BitmapDrawable;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.view.WindowManager;
import android.widget.AdapterView;
import android.widget.ListView;
import android.widget.PopupWindow;

import com.example.administrator.szb.R;
import com.example.administrator.szb.adapter.FuJianAdapter;

import java.util.List;

/**
 * Created by stone on 2018/2/5.
 */

public class FuJianPop {
    private static PopupWindow popupWindow;
    private static Activity context;

    public static void show(Activity ctx, View view, List<String> list, AdapterView.OnItemClickListener listener){
        context  = ctx;
        if (popupWindow==null){
            final View popView = LayoutInflater.from(context).inflate(
                    R.layout.fujianpop, null);
            ListView listView = popView.findViewById(R.id.listView);
            listView.setOnItemClickListener(listener);
            listView.setAdapter(new FuJianAdapter(view.getContext(),list));
            popupWindow = new PopupWindow(popView, ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);

            popupWindow.setBackgroundDrawable(new BitmapDrawable());
            popupWindow.setFocusable(true);// 点击空白处时，隐藏掉pop窗口
            popupWindow.setClippingEnabled(false);
            popupWindow.setOnDismissListener(new PopupWindow.OnDismissListener() {
                @Override
                public void onDismiss() {
                    setBackgroundAlpha(1.0f);
                }
            });
        }
        setBackgroundAlpha(0.5f);
        if (!popupWindow.isShowing())
        popupWindow.showAtLocation(view, Gravity.CENTER,0,0);
    }

    public static void hide(){
        if (popupWindow!=null){
            popupWindow.dismiss();
        }
    }


    public static void setBackgroundAlpha(float bgAlpha) {
        WindowManager.LayoutParams lp = (context).getWindow()
                .getAttributes();
        lp.alpha = bgAlpha;
        ((Activity) context).getWindow().setAttributes(lp);
    }
}
